package com.web.controller;


import com.github.pagehelper.PageInfo;
import com.web.entity.Doctor;
import com.web.entity.Result;
import com.web.service.DoctorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController
@RequestMapping("/doctor")
public class DoctorController {

    @Autowired
    private DoctorService doctorService;

    //分页查询
    //http://localhost:8080/doctor/list?page=1&pageSize=5&doctorName=赵
    @GetMapping("/list")
    public Result list(Integer page, Integer pageSize, Doctor doctor){
        log.info("分页查询");
        PageInfo<Doctor> list = this.doctorService.list(page, pageSize, doctor);
        return Result.success(list);
    }

    //http://localhost:8080/doctor/login?phone=17700177000&password=123456
    @GetMapping("/login")
    public Result login(Doctor doctor){
        log.info("登录");
        Boolean doctor1 = this.doctorService.login(doctor);
        if (doctor1){
            return Result.success("登录成功");
        }
        return Result.error("用户名或密码错误");
    }

    //http://localhost:8080/doctor/byPhone?phone=17700177232
    @GetMapping("/findPhone")
    public Result byPhone(Doctor doctor){
        log.info("查询手机号");
        Boolean doctor1 = this.doctorService.byPhone(doctor);
        if (doctor1){
            return Result.success("手机号已存在");
        }
        return Result.error("手机号不存在");
    }

    //http://localhost:8080/doctor/findById?doctorId=1
    @GetMapping("/findById")
    public Result findById(Integer doctorId){
        log.info("查询用户");
        Doctor doctor = this.doctorService.findById(doctorId);
        return Result.success(doctor);
    }

    //http://localhost:8080/doctor/add
    @PostMapping("/add")
    public Result add(@RequestBody Doctor doctor){
        log.info("新增用户");
        this.doctorService.add(doctor);
        return Result.success("新增成功");
    }

    //http://localhost:8080/doctor/update
    @PutMapping("/update")
    public Result update(@RequestBody Doctor doctor){
        log.info("修改用户");
        this.doctorService.update(doctor);
        return Result.success("修改成功");
    }

    //http://localhost:8080/doctor/delete/53
    @DeleteMapping("/delete/{doctorId}")
    public Result delete(@PathVariable Integer doctorId){
        log.info("删除用户");
        this.doctorService.delete(doctorId);
        return Result.success("删除成功");
    }


}
